{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ad58e331",
   "metadata": {},
   "source": [
    "如果列表容量很大，会占用很大的存储空间。同时我们只需要访问列表中的几个元素，那么会造成空间的浪费。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7294f0f2",
   "metadata": {},
   "source": [
    "列表元素可以按照某种算法推算出来，我们可以在循环过程中不断推算出后续的元素。这样不必创建完整的list，从而节省大量空间。\n",
    "这种一边循环一边计算的机制，称为生成器：generator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "581e8394",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L = [x * x for x in range(10)]\n",
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2affc9cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<generator object <genexpr> at 0x000001D1DBEDEC80>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = (x * x for x in range(10))\n",
    "g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6b61ca7e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(g)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e83141be",
   "metadata": {},
   "source": [
    "通过next不断读取生成器中内容。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5edb397d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "4\n",
      "9\n",
      "16\n",
      "25\n",
      "36\n",
      "49\n",
      "64\n",
      "81\n"
     ]
    }
   ],
   "source": [
    "g = (x * x for x in range(10))\n",
    "for n in g:\n",
    "    print(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c3f585e",
   "metadata": {},
   "source": [
    "generator非常强大，如果推算的算法比较复杂，用类似列表生成式的for循环无法实现，可以用函数来实现。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "82c366c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(max):\n",
    "    n, a, b = 0, 0, 1\n",
    "    while n < max:\n",
    "        print(b)\n",
    "        a,b = b, a+b\n",
    "        n = n + 1\n",
    "    return 'done'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a666e676",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'done'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fib(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46db1d48",
   "metadata": {},
   "source": [
    "fib函数实际上是定义了斐波那契数列的推算规则，与gennerator只有一步之遥\n",
    "只需把print(b)改为yield b即可"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef943738",
   "metadata": {},
   "source": [
    "这是定义generator的另一种方法，如果函数定义里包含yeild，则该函数就是生成器函数，调用生成器函数，返回的是一个生成器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "43103594",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(max):\n",
    "    n, a, b = 0, 0, 1\n",
    "    while n < max:\n",
    "        yield b\n",
    "        a,b = b, a+b\n",
    "        n = n + 1\n",
    "    return 'done'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "758e3fb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<generator object fib at 0x000001D1DBFEB350>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = fib(6)\n",
    "f"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5529b863",
   "metadata": {},
   "source": [
    "generator和普通函数的执行流程不一样。\n",
    "普通函数是顺序执行，遇到return语句或者最后一行函数语句就返回。\n",
    "generator则是在每次调用next的时候执行，遇到yield语句返回。再次调用next，从上次返回的yield处继续执行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "90a0b6a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def odd():\n",
    "    print('step 1')\n",
    "    yield 1\n",
    "    print('step 2')\n",
    "    yield(3)\n",
    "    print('step 3')\n",
    "    yield(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ef5559e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "step 1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "o = odd()\n",
    "next(o)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c4992d1f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "step 2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(o)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bdd0534a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "step 3\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(o)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ecb146a",
   "metadata": {},
   "source": [
    "请注意调用generator函数会创建一个gennerator对象，多次调用generator函数会创建多个相互独立的gennerator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "38a5f86e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "for n in fib(6):\n",
    "    print(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06586247",
   "metadata": {},
   "source": [
    "### 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29f5aaf3",
   "metadata": {},
   "source": [
    "注意普通函数和生成器函数的区别。遇到return语句或执行到函数体最后一行语句，就是结束generator的指令。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44de0829",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
